package jdbc1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

/**
 * @author oldliu
 * @since 1.0
 */
public class TEstAdd2 {
    public static void main(String[] args) {
        try {
            //static{}执行了
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return;
        }
        //ip信息，端口，库名，其他参数
        //jdbc:mysql://localhost:3308/java83
        String url = "jdbc:mysql://localhost:3308/java83?useUnicode=true&characterEncoding=utf8&useSSL=false" +
                "&serverTimezone=Asia/Shanghai";
        Connection con = null;
        Statement st = null;
        Scanner in = new Scanner(System.in);
        System.out.print("请输入新课名：");
        String newName = in.nextLine();
        System.out.print("请输入课时：");
        int hour = in.nextInt();
        System.out.print("请输入年级号：");
        long gid = in.nextLong();
        try {
            con = DriverManager.getConnection(url, "xukang", "Xu1234");
            System.out.println(con.getClass().getName());
            st = con.createStatement();
            String sql = "insert into s_subject(subjectname,classhour,gradeid)" +
                    "values('"+newName+"',"+hour+","+gid+")";
            System.out.println(sql);
            int line = st.executeUpdate(sql);
            System.out.println("成功增加了" + line + "行");

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {

            try {
                if (st != null)
                    st.close();

            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {

                if (con != null)
                    con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

    }
}
